package com.logic.landseaserver.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.logic.landseaserver.service.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.logic.common.exception.BusinessException;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.enums.PropertyStatusEnum;
import com.logic.landseaserver.common.exception.LandseaBusinessException;
import com.logic.landseaserver.domain.PropertyDecision;
import com.logic.landseaserver.persistence.read.PropertyDecisionReadMapper;
import com.logic.landseaserver.persistence.write.PropertyDecisionWriteMapper;
import com.logic.landseaserver.service.IProperty;
import com.logic.landseaserver.service.IPropertyDecision;
import com.logic.landseaserver.ws.dto.PropertyDecisionDTO;
import com.logic.landseaserver.ws.request.PropertyDecisionReq;
import com.logic.landseaserver.ws.response.PropertyDetailResp;
import com.logic.system.domain.SystemFile;
import com.logic.system.service.IFileService;
import com.logic.system.ws.dto.FileConverter;


@Service
public class PropertyDecisionServiceImpl implements IPropertyDecision {

    @Autowired
    private PropertyDecisionReadMapper propertyDecisionReadMapper;
    @Autowired
    private IProperty propertyService;
    @Autowired
    private PropertyDecisionWriteMapper propertyDecisionWriteMapper;
    @Autowired
    private IFileService fileService;

    @Override
    @Action(name ="上传投决会议记录")
    public int insert(Object o) throws BusinessException {
        Integer userId = SecurityUtils.getCurrentLogin().getUserId();
        if (userId == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.TOKEN_IS_NULL);
        }
        PropertyDecisionReq req = (PropertyDecisionReq) o;
        //输入校验
        this.validateForInsert(req);
        PropertyDecision propertyDecision = PropertyDecisionReq.convertFromDTO(req);
        //业务流程校验
        checkTheProperty(propertyDecision.getPropertyId());
        propertyDecision.setSummaryVersion(propertyDecisionReadMapper.getMaxVersion(propertyDecision.getPropertyId()));
        propertyDecision.updateCommonInfo(userId);
        propertyDecisionWriteMapper.insertSelective(propertyDecision);
        return propertyDecision.getId();
    }

    private void checkTheProperty(Integer propertyId) throws BusinessException {
        Object object = propertyService.get(propertyId);

        if (object == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DELETE_PROPERTY_ID_IS_NULL);
        }
        PropertyDetailResp property = (PropertyDetailResp) object;
        //是否中止
        if (property.getSuspend()) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_HAS_SUSPEND);
        }
        //是否是投决阶段
        if (!property.getStatus().equals(PropertyStatusEnum.PS3.toString())) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_SIGN_ISNOT_PS4);
        }
    }


    @Override
    public Object get(Integer integer) throws BusinessException {
        return null;
    }

    @Override
    public void update(Object o) throws BusinessException {

    }

    @Override
    public void delete(String s) throws BusinessException {

        Integer userId = SecurityUtils.getCurrentLogin().getUserId();
        if (userId == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.TOKEN_IS_NULL);
        }
        if (StringUtils.isEmpty(s)) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DELETE_IDS_IS_NULL);
        }
        if (s.split(",").length == 0) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DELETE_IDS_IS_NULL);
        }

        Map map = new HashMap<>();
        map.put("userId", userId);
        map.put("list", s.split(","));
        propertyDecisionWriteMapper.delete(map);

    }

    @Override
    public List<?> query(QueryUtil queryUtil) {
        return null;
    }

    @Override
    public Boolean validate(Object o) throws BusinessException {
        return null;
    }

    @Override
    public Boolean validateForInsert(Object o) throws BusinessException {
        PropertyDecisionReq req = (PropertyDecisionReq) o;

        if (req.getPropertyId() == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DECISION_CODITION_ISNULL);
        }
        if (StringUtils.isEmpty(req.getBusinessCondition())) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DECISION_CODITION_ISNULL);
        }
        if (req.getSummaryId() == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DECISION_FILE_ISNULL);
        }
        if (StringUtils.isEmpty(req.getSummaryName())) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DECISION_FILENAME_ISNULL);
        }
        if (req.getDecisionDate() == null) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DECISION_DATE_ISNULL);
        }

        if (StringUtils.isEmpty(req.getDecisionResult())) {
            throw new LandseaBusinessException(LandseaErrorMessageEnum.PROPERTY_DECISION_RESULT_ISNULL);
        }


        return null;
    }

    @Override
    public Boolean validateForUpdate(Object o) throws BusinessException {
        return null;
    }

    @Override
    public Boolean validateForDelete(Object o) throws BusinessException {
        return null;
    }

    @Override
    public List<PropertyDecisionDTO> queryPage(QueryUtil util) throws BusinessException {
        List<PropertyDecisionDTO> dtoList = propertyDecisionReadMapper.queryListPage(util);
        for (int i = 0; dtoList.size() > 0 && i < dtoList.size(); i++) {
            PropertyDecisionDTO dto = dtoList.get(i);
            dto.setSummaryFile(FileConverter.fromFileToDTO((SystemFile) this.fileService.get(dto.getSummaryId())));
        }
        return dtoList;
    }
}
